clear 

* set directory 
* cd "directory"

* set paths
global datafolder "data"
global outputfolder "output"

********************************************************************************
*			Table 7: Future credit ratings
********************************************************************************

	use "$datafolder\maindata.dta", clear

	keep if datem >= 660
	
	global cvars "OROA lDeposits lTotalAssets ratio_CurrentLiabilities "
	global loanChars "maturity_range has_coll_m_any lbalance"
	
	collapse (mean) Rating rating_*mAfter Dif Industry actual_rate theoretical_rate dateq $cvars $loanChars, ///
	by(customerid datem)
	
	quietly egen firmid = group(customerid)
	quietly egen dateqid = group(dateq)
	
	label variable Dif "Dif"
	label variable Rating "Rating"
	label variable actual_rate "Actual Rate"
	label variable theoretical_rate "Breakeven Rate"
	
	sort customerid dateq
	bysort customerid: gen Dif_first_x = Dif if _n==1
	bysort customerid: egen Dif_first = max(Dif_first_x)
	
	* Baseline: all firms
	estimates clear 
	
	reghdfe rating_12mAfter Dif Rating , ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M1
	
	reghdfe rating_24mAfter Dif Rating , ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M2
	
	reghdfe rating_36mAfter Dif Rating, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M3
	
	reghdfe rating_48mAfter Dif Rating , ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M4
	
	reghdfe rating_60mAfter Dif Rating , ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M5
	
	
	esttab * using "$outputfolder\table_7_A.tex", replace ///
	noconstant ///
	se r2 label star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) r(3) nonotes ///
	mtitles("h=12" "h=24" "h=36" "h=48" "h=60") /// 
	scalars("hasq Quarter FE" "hasind Industry FE" "hasfirm Firm FE")
	
	
	
	estimates clear 
			
	reghdfe rating_12mAfter Dif Rating if Dif_first<=0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M1
	
	reghdfe rating_24mAfter Dif Rating if Dif_first<=0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M2
	
	reghdfe rating_36mAfter Dif Rating if Dif_first<=0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M3
	
	reghdfe rating_48mAfter Dif Rating if Dif_first<=0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M4
	
	reghdfe rating_60mAfter Dif Rating if Dif_first<=0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M5
	
	
	esttab * using "$outputfolder\table_7_B.tex", replace keep(Dif Rating) ///
	se r2 label star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) r(3) nonotes ///
	mtitles("h=12" "h=24" "h=36" "h=48" "h=60") /// 
	scalars("hasq Quarter FE" "hasind Industry FE" "hasfirm Firm FE")
	
	
	estimates clear 
			
	reghdfe rating_12mAfter Dif Rating if Dif_first>0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M1
	
	reghdfe rating_24mAfter Dif Rating if Dif_first>0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M2
	
	reghdfe rating_36mAfter Dif Rating if Dif_first>0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M3
	
	reghdfe rating_48mAfter Dif Rating if Dif_first>0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M4
	
	reghdfe rating_60mAfter Dif Rating if Dif_first>0, ///
	abs(Industry dateq) cluster(customerid)
	quietly estadd local hasq "Yes"
	quietly estadd local hasind "Yes"
	quietly estadd local hasfirm "No"
	estimates store M5
	
	
	esttab * using "$outputfolder\table_7_C.tex", replace keep(Dif Rating) ///
	se r2 label star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) r(3) nonotes ///
	mtitles("h=12" "h=24" "h=36" "h=48" "h=60") /// 
	scalars("hasq Quarter FE" "hasind Industry FE" "hasfirm Firm FE")
	
	